angular.module('app.MemberController', [])

    .controller('MemberController',function ($scope, API) {
        //$location.path('/member/index');
        $scope.$emit('hideNav',false);

    })

    .controller('MemberIndexController',function ($scope, API) {

        API.getUser(null,function(err,result){
            $scope.userinfo = result;
            $scope.userinfo.isconnected = ($scope.userinfo.isconnected)?'已关注':'未关注';
        });
        $scope.$emit('hideNav',false);

    })

    .controller('MemberDetailController',function ($scope, API) {
        $scope.page = 1;
        $scope.ChildAccount = {};
        $scope.islast = true;
        $scope.moreDataCanBeLoaded = function(){
            return $scope.islast;
        };
        $scope.loadMore = function(){
            API.getChildAccount({
                pagesize : 15,
                page : $scope.page
            },function(err,result){
                $scope.total_money = result.data.total_money;
                if(result.data.ChildAccount.length > 0){
                    var ChildAccount = result.data.ChildAccount;
                    Array.prototype.push.apply($scope.ChildAccount,ChildAccount);
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $scope.page++;
                }
                $scope.islast = result.data.pages.isLast ? false : true;
            });
        };


        // $scope.loadMore();

    })

    .controller('MemberXioafeiController',function ($scope, API) {
        API.getConsuAccount({
            pagesize : 100
        },function(err,result){
            $scope.SysPostal = result.data.SysPostal;
            $scope.VipPostal = result.data.VipPostal;
            var ConsuAccount = result.data.ConsuAccount;
            for(x in ConsuAccount){
                if(ConsuAccount[x].type == 1){
                    ConsuAccount[x].type = 'VIP';
                }else{
                    ConsuAccount[x].type = '余额';
                }
            }
            $scope.ConsuAccount = ConsuAccount;
        });


    })

    .controller('MemberInfoController',function ($scope,API,fileReader,Upload,ConfigModule,$state,$ionicLoading,loadConfig) {
        API.getUser(null,function(err,result){
            if(result){
                $scope.userinfo = result;
            }
        });
        $scope.getFile = function(file){
            $ionicLoading.show(loadConfig);
            Upload.upload({
                url : ConfigModule.editUser,
                data : {
                    avatar : file
                }
            }).then(function(){
                $ionicLoading.hide();
                $state.reload();
            });
        };

        $scope.$emit('hideNav',false);

    })

    .controller('MemberPlaceController',function ($scope,API) {
        API.getUser(null,function(err,result){
            $scope.userinfo = result;
            $scope.userinfo.isconnected = ($scope.userinfo.isconnected)?'已关注':'未关注';
        });
        API.getChildCount(function(err,result){
            $scope.ChildCount = result.ChildCount;
        });
        API.getPostal(function(err,result){
            $scope.money = result.money;
        });
        $scope.$emit('hideNav',false);

    })

    .controller('MemberQrcodeController', function ($scope,API) {
        API.share_qrcode(function(err,result){
            if(result){
                $scope.qrcode = result.data.url;
            }
        });
        $scope.$emit('hideNav',false);
    })

    .controller('MemberQrcodeCreateController',function ($scope,API,$state,fileReader,Upload,ConfigModule,$ionicLoading,loadConfig) {
        $scope.$emit('hideNav',false);
        API.get_qrcode(function(err,result){
            if(result.data.qrcode){
                $scope.qrcode = result.data.qrcode;
            }else{
                $scope.qrcode = {};
            }
        });
        API.getUser(null,function(err,result){
            if(result){
                $scope.userinfo = result;
            }
        });
        $scope.submit = function(){
            API.update_qrcode({
                qrcode : $scope.qrcode.qrcode,
                nickname : $scope.userinfo.nickname,
            },function(err,result){
                $state.go('member.qrcode');
            });
        };
        $scope.getFile = function(file){
            $ionicLoading.show(loadConfig);
            Upload.upload({
                url : ConfigModule.editUser,
                data : {
                    avatar : file
                }
            }).then(function(){
                $ionicLoading.hide();
                $state.reload();
            });
        };
    })

    .controller('MemberParentController',function ($scope,API) {
        $scope.$emit('hideNav',true);
        API.getUser(null,function(err,result){
            $scope.userinfo = result;
        });
        API.getParentList(function(err,result){
            if(result) {
                if (result.FirstUser) {
                    $scope.FirstUser = result.FirstUser;
                    switch (result.FirstUser.sex){
                        case '男' :
                            $scope.FirstUser.sex = '男';
                            $scope.FirstUser.sexclass = 'man';
                            break;
                        case '女' :
                            $scope.FirstUser.sex = '女';
                            $scope.FirstUser.sexclass = 'woman';
                            break;
                        case '' :
                            $scope.FirstUser.sex = '';
                            $scope.FirstUser.sexclass = 'wgz';
                            break;
                        default:
                            $scope.FirstUser.sex = '';
                            $scope.FirstUser.sexclass = 'wgz';
                            break;
                    }
                    if(result.FirstUser.isconnected == '关注'){
                        $scope.FirstUser.gz = 'gz';
                    }else{
                        $scope.FirstUser.gz = 'wgz';
                    }
                }
                if (result.SecondUser) {
                    $scope.SecondUser = result.SecondUser;
                    switch (result.SecondUser.sex){
                        case '男' :
                            $scope.SecondUser.sex = '男';
                            $scope.SecondUser.sexclass = 'man';
                            break;
                        case '女' :
                            $scope.SecondUser.sex = '女';
                            $scope.SecondUser.sexclass = 'woman';
                            break;
                        case '' :
                            $scope.SecondUser.sex = '';
                            $scope.SecondUser.sexclass = 'wgz';
                            break;
                        default:
                            $scope.SecondUser.sex = '';
                            $scope.SecondUser.sexclass = 'wgz';
                            break;
                    }
                    if(result.SecondUser.isconnected == '关注'){
                        $scope.SecondUser.gz = 'gz';
                    }else{
                        $scope.SecondUser.gz = 'wgz';
                    }
                }
                if (result.Thirduser) {
                    $scope.Thirduser = result.Thirduser;
                    switch (result.Thirduser.sex){
                        case '男' :
                            $scope.Thirduser.sex = '男';
                            $scope.Thirduser.sexclass = 'man';
                            break;
                        case '女' :
                            $scope.Thirduser.sex = '女';
                            $scope.Thirduser.sexclass = 'woman';
                            break;
                        case '' :
                            $scope.Thirduser.sex = '';
                            $scope.Thirduser.sexclass = 'wgz';
                            break;
                        default:
                            $scope.Thirduser.sex = '';
                            $scope.Thirduser.sexclass = 'wgz';
                            break;
                    }
                    if(result.Thirduser.isconnected == '关注'){
                        $scope.Thirduser.gz = 'gz';
                    }else{
                        $scope.Thirduser.gz = 'wgz';
                    }
                }

            }
        });

    })

    .controller('MemberAllyController',function ($scope,API,$stateParams) {
        $scope.$emit('hideNav',true);
        var type = $stateParams.type;
        if(!type) type = 'all';
        if(type == 'first'){
            $scope.type = true;
        }
        $scope.page = 1;
        $scope.childList = {};
        $scope.islast = true;
        $scope.moreDataCanBeLoaded = function(){
            return $scope.islast;
        };
        $scope.onload = function(){
            API.getChildList({
                level:type,
                pagesize : 10,
                page : $scope.page
            },function(err,result){
                if(result.ChildList.length > 0){
                    Array.prototype.push.apply($scope.childList, result.ChildList);
                    $scope.islast = result.islast;
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    // $scope.childList = result;
                    for(x in $scope.childList){
                        if($scope.childList[x].isconnected == '关注'){
                            $scope.childList[x].gz = 'gz';
                        }else{
                            $scope.childList[x].gz = 'wgz';
                        }
                        if($scope.childList[x].sex == '男'){
                            $scope.childList[x].sex_class = 'man';
                        }else if($scope.childList[x].sex == '女'){
                            $scope.childList[x].sex_class = 'woman';
                        }else{
                            $scope.childList[x].sex_class = 'wgz';
                        }
                    }
                    $scope.page++;
                }else{

                    $scope.islast = result.islast;
                }
            });
        };

        $scope.onload();
        $scope.loadMore = function(){
            $scope.onload();
        }
        $scope.$on('stateChangeSuccess', function() {
            $scope.loadMore();
        });
    })

    .controller('MemberWithdrawController',function ($scope,API,$ionicPopup,$state) {
        $scope.cash = '';
        $scope.$emit('hideNav',true);
        API.getPostal(function(err,result){
            $scope.money = result.money;
        });
        $scope.alert = function(msg){
            var cancels = $ionicPopup.confirm({
                title: '消息提示',
                template:msg,
                buttons: [
                    { text: '<b>确定</b>',
                        type: 'button-positive'
                    }
                ]
            });

            cancels.then(function(res) {
                $state.reload();
            });
        };
        $scope.postal = function(cash){
            if(cash <= 0 || isNaN(cash)){
                return $scope.alert('请输入正确的数字');
            }
            if(cash < 20 || cash > 200){
                return $scope.alert('请输入20~200之间的数字');
            }
            API.cashPostal({
                money : cash
            },function(err,result){
                if(result.status == 200){
                    $scope.alert('提现成功,请查看微信账户');
                }else{
                    $scope.alert(result.message);
                }
            });
        }
    })

    .controller('MemberMobileController', '$ionicPopup',function ($scope,API,$ionicPopup) {
        $scope.$emit('hideNav',false);
        $scope.showPopup = function() {
            $scope.data = {}
            // 自定义弹窗
            var myPopup = $ionicPopup.show({
                template: '<input type="password" ng-model="data.mobile">',
                title: '请输入新的手机号',
                scope: $scope,
                buttons: [
                    {text: '取消'},
                    {
                        text: '<b>保存</b>',
                        type: 'button-positive',
                        onTap: function (e) {
                            if (!$scope.data.mobile) {
                                e.preventDefault();
                            } else {
                                return $scope.data.mobile;
                            }
                        }
                    },
                ]
            })
        }
    })

    .controller('MemberPersonController', function ($scope,API,$ionicNavBarDelegate,$stateParams,$state) {
        $scope.$emit('hideNav',true);
        $scope.goBack = function() {
            $ionicNavBarDelegate.back();
        };
        if( !$stateParams.id ){
            return $state.go('member.index');
        }
        API.getUser({user_id:$stateParams.id},function(err,result){
            if(result){
                $scope.userinfo = result;
                $scope.userinfo.gz =  ($scope.userinfo.isconnected)?'gz':'wgz';
                $scope.userinfo.isconnected = ($scope.userinfo.isconnected)?'已关注':'未关注';
            }
        });
    })

    .controller('MemberEditInfoController',function ($scope,API,$ionicPopup,$state) {
        API.getUser(null,function(err,result){
            $scope.userinfo = result;
            if($scope.userinfo.birthday){
                var birthday = $scope.userinfo.birthday.split('-');

                $scope.user.year = (birthday[0])?birthday[0]+'年': '1990年';
                $scope.user.month = (birthday[1])?parseInt(birthday[1])+'月':'1月';
                $scope.user.day = (birthday[2])?parseInt(birthday[2])+'日':'1日';
            }
            if($scope.userinfo.sex == '男'){
                $scope.userinfo.sex = '1';
            }else if($scope.userinfo.sex == '女'){
                $scope.userinfo.sex = '2';
            }else{
                $scope.userinfo.sex = '2';
            }
        });
        $scope.$emit('hideNav',false);
        $scope.user = {};
        $scope.user.year = '1990年';
        $scope.user.month = '1月';
        $scope.user.day = '1日';

        $scope.year = [];
        var a=0;
        for(var i=1900;i<=2016;i++){
            $scope.year[a] = i+'年';
            a++;
        }
        $scope.month = [];
        var a=0;
        for(var i=1;i<=12;i++){
            $scope.month[a] = i+'月';
            a++;
        }
        $scope.$watch('user.year', function(newValue) {
            var allday = DayNumOfMonth(newValue,$scope.user.month);
            $scope.day=[];
            var a=0;
            for(var i=1;i<=allday;i++){
                $scope.day[a] = i+'日';
                a++;
            }
        })

        $scope.$watch('user.month', function(newValue) {
            var allday = DayNumOfMonth($scope.user.year,newValue);
            $scope.day=[];
            var a=0;
            for(var i=1;i<=allday;i++){
                $scope.day[a] = i+'日';
                a++;
            }
        });
        $scope.submit = function(){
            var birthday = $scope.user.year.replace('年','')+'-'+$scope.user.month.replace('月','')+'-'+$scope.user.day.replace('日','');
            API.editUser({
                email : $scope.userinfo.email,
                birthday : birthday,
                nickname : $scope.userinfo.nickname,
                sex : $scope.userinfo.sex,
            },function(err,result){
                if(result){
                    $scope.save_success();
                }else{
                    $scope.save_err();
                }
            });
        }

        $scope.save_success = function() {
            var cancels = $ionicPopup.confirm({
                title: '保存成功',
                template: '用户信息保存成功！',
                buttons: [
                    { text: '<b>确定</b>',
                        type: 'button-positive'
                    },
                ]
            });
            cancels.then(function(res) {
                $state.go('member.info');
            });
        };
        $scope.save_err = function() {
            var cancels = $ionicPopup.confirm({
                title: '消息提醒',
                template: '用户信息保存失败，请检查数据！',
                buttons: [
                    { text: '<b>确定</b>',
                        type: 'button-positive'
                    },
                ]
            });
        };
    });




function DayNumOfMonth(Year,Month)
{
    var d = new Date(Year.replace('年',''),Month.replace('月',''),0);
    return d.getDate();
}
function PrefixInteger(num, n) {
    return (Array(n).join(0) + num).slice(-n);
}